﻿using PagedList;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Security;
using System.Linq;


namespace FB.Membership
{
	public class AspNetRoleProviderWrapper : IRolesService
	{
		private readonly RoleProvider _roleProvider;


        public static string[] RoleSession
        {
            get { return System.Web.HttpContext.Current.Session[string.Format("{0}-$ROLE", FB.Helpers.AppSettingsBase.AppKey)] as string[]; }
            set { System.Web.HttpContext.Current.Session.Add(string.Format("{0}-$ROLE", FB.Helpers.AppSettingsBase.AppKey), value); }
        }

		public AspNetRoleProviderWrapper()
		{
			if (Roles.Enabled)
				_roleProvider = Roles.Provider;
		}

		public AspNetRoleProviderWrapper(RoleProvider roleProvider)
		{
			_roleProvider = roleProvider;
		}

		#region IRolesService Members

		public bool Enabled
		{
			get { return _roleProvider != null; }
		}

		public IEnumerable<string> FindAll()
		{
			return _roleProvider.GetAllRoles();
		}


		public IEnumerable<string> FindByUser(MembershipUser user)
		{
			return _roleProvider.GetRolesForUser(user.UserName);
		}

		public IEnumerable<string> FindByUserName(string userName)
		{
			return _roleProvider.GetRolesForUser(userName);
		}

		public IEnumerable<string> FindUserNamesByRole(string roleName)
		{
			return _roleProvider.GetUsersInRole(roleName);
		}
        public IList<string> FindAllUsers(string roleName)
        {
            try
            {
                var query = from u in _roleProvider.GetUsersInRole(roleName)
                            select u;
                return query.ToList();
            }
            catch
            {
                return new List<string>();
            }
           
        }

        public IPagedList<string> FindUsersByRole(string roleName, string usernameToMatch, int pageNumber, int pageSize)
        {
            int totalUserCount;
            var users = from u in _roleProvider.GetUsersInRole(roleName)
                        select u;

            int skipRows = (pageNumber - 1) * pageSize;

            totalUserCount = users.Count();
            users = users.OrderBy(o => o).Skip(skipRows).Take(pageSize);

            return new StaticPagedList<string>(users.ToList(), pageNumber, pageSize, totalUserCount);
            
        }


		public void AddToRole(MembershipUser user, string roleName)
		{
			_roleProvider.AddUsersToRoles(new[] {user.UserName}, new[] {roleName});
		}

		public void RemoveFromRole(MembershipUser user, string roleName)
		{
			_roleProvider.RemoveUsersFromRoles(new[] {user.UserName}, new[] {roleName});
		}


        public void RemoveFromRole(List<MembershipUser> users, string roleName)
        {
            _roleProvider.RemoveUsersFromRoles(users.Select(s=>s.UserName).ToArray(), new[] { roleName });
        }


		public void RemoveFromAllRoles(MembershipUser user)
		{
			var roles = FindByUser(user);
			foreach(var role in roles)
				RemoveFromRole(user, role);
		}

		public bool IsInRole(MembershipUser user, string roleName)
		{
			return _roleProvider.IsUserInRole(user.UserName, roleName);
		}

		public void Create(string roleName)
		{
			_roleProvider.CreateRole(roleName);
		}

		public void Delete(string roleName)
		{
			_roleProvider.DeleteRole(roleName, false);
		}

		#endregion

	}
}